#include "PlayState.h"


PlayState::PlayState()
	:pain(false),
	pain2(false),
	gQuit(false),
	damage(false),
	mHitBob(0.0),
	mHitWeave(0.0),
	mCollisionDelay(3.0f)

{
}


PlayState::~PlayState(void)
{
}

void PlayState::Load()
{

	background.Load("Bob&Weave_Background.png");
	Sound.Load("POL-jet-anger-short.wav");
	Sound.Play(true);
	healthBar[0].Load();
	healthBar[1].Load();




	healthBar[0].SetPosition(SVector2(385, 67.0f));
	healthBar[1].SetPosition(SVector2(608.0f, 67.0f));



	gFighter[0].Load("bob_sprite_sheet.txt");
	gFighter[1].Load("weave_sprite_sheet.txt");
	gFighter[1].SetFacingLeft(true);

	gFighter[0].SetPosition(SVector2(220.0f, 300.0f));
	gFighter[1].SetPosition(SVector2(240.0f, 300.0f));

	gFighter[0].SetJabRightKey(Keys::D);
	gFighter[1].SetJabRightKey(Keys::J);

	gFighter[0].SetJabLeftKey(Keys::S);
	gFighter[1].SetJabLeftKey(Keys::K);

	gFighter[0].SetHookRightKey(Keys::E);
	gFighter[1].SetHookRightKey(Keys::U);

	gFighter[0].SetHookLeftKey(Keys::W);
	gFighter[1].SetHookLeftKey(Keys::I);

	gFighter[0].SetDodgeRightKey(Keys::A);
	gFighter[1].SetDodgeRightKey(Keys::L);

	gFighter[0].SetDodgeLeftKey(Keys::F);
	gFighter[1].SetDodgeLeftKey(Keys::H);

	gFont.Load(24);
}

void PlayState::Unload()
{
	Sound.Unload();
	gFighter[0].Unload();
	gFighter[1].Unload();

	healthBar[0].Unload();
	healthBar[1].Unload();

	gFont.Unload();
	background.Unload();
}

NextState PlayState::Update(float deltaTime)
{
	gFighter[0].Update(deltaTime);
	gFighter[1].Update(deltaTime);
	NextState nextState = None;
	mCollisionDelay-=deltaTime;

	pain = (   Intersect(gFighter[0].GetBoundingBox(), gFighter[1].GetDamageBox())   );

	if (pain)
	{


		healthBar[0].SetHit(true);
		gFighter[0].ChangeState(AS_Hurt);
		float newHealth=healthBar[0].GetCurHealth()-.1f;
		healthBar[0].SetCurHealth(newHealth);
		healthBar[0].SetPosition(SVector2(350, 67.0f));

		if (mCollisionDelay <= 0.0f)
		{
			++mHitBob;
			mCollisionDelay=3;
		}
		if ( mHitBob==5.0)
		{
			nextState = Frontend;
		}
	}

	pain2 = (Intersect(gFighter[1].GetBoundingBox(), gFighter[0].GetDamageBox()));

	if (pain2)
	{

		healthBar[1].SetHit(true);
		gFighter[1].ChangeState(AS_Hurt);

		float newHealth=healthBar[0].GetCurHealth()-.1f;
		healthBar[1].SetCurHealth(newHealth);
		healthBar[1].SetPosition(SVector2(576, 67.0f));

		if (mCollisionDelay <= 0.0f)
		{
			++mHitWeave;
			mCollisionDelay=3;
		}
		if ( mHitWeave==5.0)
		{
			nextState = Frontend;
		}
	}

	healthBar[0].Update(deltaTime);
	healthBar[1].Update(deltaTime);



	if (Input_IsKeyPressed(Keys::ESCAPE))
	{
		nextState = Frontend;
	}

	return nextState;
}

void PlayState::Render()
{
	background.Render();

	const SVector2 kTarget = gFighter[0].GetPosition();

	const int kScreenWidth = IniFile_GetInt("WinWidth", 800);
	const int kScreenHeight = IniFile_GetInt("WinHeight", 600);


	const SVector2 kTarget1 = gFighter[1].GetPosition();

	gFighter[0].Render(kTarget);
	gFighter[1].Render(kTarget1);

	healthBar[0].Render(kTarget);
	healthBar[1].Render(kTarget1);

	/*Graphics_DebugRect(gFighter[0].GetBoundingBox(), 0xff0000);
	Graphics_DebugRect(gFighter[1].GetBoundingBox(), 0xff0000);
	Graphics_DebugRect(gFighter[0].GetDamageBox(), 0x0000ff);
	Graphics_DebugRect(gFighter[1].GetDamageBox(), 0x0000ff);*/

	gFont.Print(gFighter[0].GetCurrentState(), 0, 30);
	gFont.Print(gFighter[1].GetCurrentState(), 750, 30);

	if (pain)
	{
		gFont.Print("ouch!!", 0, 500);
	}
}
